<?php

namespace App\Models;

use App\Http\Library\Map;
use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

/**
 * App\Models\Area
 *
 * @property int $id ID
 * @property int|null $pid 父id
 * @property string|null $shortname 简称
 * @property string|null $name 名称
 * @property string|null $mergename 全称
 * @property int|null $level 层级 0 1 2 省市区县
 * @property string|null $pinyin 拼音
 * @property string|null $code 长途区号
 * @property string|null $zip 邮编
 * @property string|null $first 首字母
 * @property string|null $lng 经度
 * @property string|null $lat 纬度
 * @method static \Illuminate\Database\Eloquent\Builder|Area newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Area newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Area query()
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereFirst($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereMergename($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area wherePinyin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereShortname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereZip($value)
 * @mixin \Eloquent
 */
class Area extends Model
{
	use HasDateTimeFormatter;
    public $timestamps = false;

    public static function getOptions($pid = 0, $isThree = true): array
    {
        $list = self::query()->where("pid",$pid)->get(['id','name','level'])->toArray();
        if ($list){
            foreach ($list as $key=>$item){
                if ($isThree){
                    $children = self::getOptions($item['id']);
                    if ($children){
                        $item['children'] = $children;
                    }
                    $list[$key] = $item;
                }else{
                    $children = self::query()->where("pid",$item['id'])->get(['id','name'])->toArray();
                    if ($children){
                        $item['children'] = $children;
                    }
                    $list[$key] = $item;
                }
            }
        }
        return $list;
    }

    public static function findAddress($longitude, $latitude){
        $key = md5("new_tencent_address_price_".$longitude."_".$latitude);
        $area_id = Cache::get($key, 0);
        if (!$area_id){
            $bd = new Map();
            $address = $bd->getAddress($longitude,$latitude);
            $area_id = 0;
            if ($address){
                unset($address['address']);
                unset($address['result']);
                $mergename = implode(",",$address);
                $area = Area::query()->where("name",$address['district'])->where("mergename","like","%{$mergename}%")->first();
                if ($area){
                    $area_id = $area->id;
                    Cache::set($key,$area_id, now()->addDay());
                }
            }
        }
        return $area_id;
    }

}
